﻿#region License
/*

	Copyright (C) 2008 Oliver Charles, Daniel Hollander

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
*/
#endregion

using System;
using System.Drawing;
using Moebius.Hud;
using SlimDX.Direct3D9;

namespace Moebius.Rendering.Direct3D9
{
    public sealed class Font : Hud.Font, IDisposable
    {
        private readonly SlimDX.Direct3D9.Font internalFont;

        internal Font(Device direct3DDevice, FontSettings settings)
        {
            internalFont = new SlimDX.Direct3D9.Font(direct3DDevice, settings.Size.Height, settings.Size.Width,
                                                     FontWeight.Regular, 0, settings.Italic, CharacterSet.Default, Precision.TrueTypeOnly,
                                                     FontQuality.ClearType, PitchAndFamily.Default, settings.Family);
        }

        public override Hud.TextDisplay CreateText(string contents, Point location)
        {
            TextDisplay newText = new TextDisplay(this);
            newText.Contents = contents;
            newText.Location = location;

            return newText;
        }

        public void DrawText(string contents, Point location)
        {
            if (internalFont != null)
                internalFont.DrawString(null, contents, location.X, location.Y, Color.White);
        }

        private bool disposed;

        private void Dispose(bool disposing)
        {
            if(disposing)
            {
                if(!disposed)
                    internalFont.Dispose();

                disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~Font()
        {
            Dispose(false);
        }
    }
}